package org.itboys.mobile.controller;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayApkInfo;
import org.itboys.mobile.service.mongo.mobile.SundayMobileApkInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：jiangxiong
 * 日期：2017年2月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_更新APK_controller。
 */
@Controller
@RequestMapping("/sunday/mobile/apk")
public class SundayMobileApkController extends BaseController {
        @Autowired
        private SundayMobileApkInfoService apkInfoService;
        @Autowired
        private ResourceHolder resourceHolder;



        @RequestMapping(value="/getApkInfo", method=RequestMethod.POST)
        @ApiOperation(value="获取最新版本版本信息。0有更新，-2无更新，-1系统错误")
        public void getApkInfo(HttpServletResponse response,
                       @ApiParam(required=true, name="type", value="1，安卓用户版本，2，安卓管理版，3，苹果用户版，4，苹果管理版")@RequestParam("type") Integer type,
                       @ApiParam(required=true, name="versionCode", value="版本号。")@RequestParam("versionCode") Integer versionCode) {
            try {
                SundayApkInfo apkInfo=  apkInfoService.getApkInfo(versionCode, type);
                if(apkInfo==null){
                    AjaxUtils.renderJsonporJson(response, null,
                            ResultPageDOHelper.getMsgCodeResultDO(
                                   -2, "没有新版本"));
                }else{
                    AjaxUtils.renderJsonporJson(response, null,
                            ResultPageDOHelper.getMsgResultDO(apkInfo,CommonConstants.SUCCESS));
                }
            } catch (Exception e) {
                String msg ="";
                if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                    msg=e.getMessage();
                }else{
                    msg= CommonConstants.ERROMSG;
                    logger.info("/sunday/mobile/apk/upload--------"+e);
                    e.printStackTrace();
                }

                AjaxUtils.renderJsonporJson(response, null,
                        ResultPageDOHelper.getMsgCodeResultDO(
                                CommonConstants.FAIL_CODE,msg));

            }
        }

    /**
     * a
     * @param request
     * @param model
     * @param type,1,用户版，2，管理版
     * @return
     */
    @RequestMapping(value="/download",method={RequestMethod.POST,RequestMethod.GET})
    public  String download(HttpServletRequest request,
                            Model model,
                            @RequestParam(value="type",required=true) Integer type){
        String  title="花果山果蔬";
        if(type==1){
            //用户版
            title+="（用户版）";
        }else if(type==2){
            //管理版
            title+="（管理版）";
        }
        model.addAttribute("title",title);
        //按照下载类型，数据库中查询
        String  androidUrl=null;
        if(type==1){
            //安卓用户版
            androidUrl=apkInfoService.getLastedDownUrl(1);
        }else if(type==2){
            //安卓管理版
            androidUrl=apkInfoService.getLastedDownUrl(2);
        }
        model.addAttribute("androidUrl",androidUrl);

        String iosUrl = "";
        if(type==1){
            //苹果用户版
            iosUrl=apkInfoService.getLastedDownUrl(3);
            //  iosUrl="https://itunes.apple.com/cn/app/优资融/id1232238821";
        }else if(type==2){
            //苹果管理版
            iosUrl=apkInfoService.getLastedDownUrl(4);
            //  iosUrl="https://itunes.apple.com/cn/app/淘宝-随时随地-想淘就淘/id387682726";
        }
        logger.info("---最新IOSURL="+iosUrl);
        model.addAttribute("iosUrl",iosUrl);
        //二维码（或项目LOGO）,请保证目录存有此文件。
        String logoUrl=resourceHolder.getStringValue("imgRoot")+"/apk/ewm/";
        if(type==1){
            logoUrl+="/yonghuban.png";
        }else if(type==2){
            logoUrl+="/guanliban.png";
        }
        model.addAttribute("logoUrl",logoUrl);
        return "/sunday/apk/download";
    }

}
